package com.starblink.android.common.route.provider

import android.content.Context
import com.alibaba.android.arouter.facade.annotation.Route
import com.starblink.android.basic.aroute.AccountProvider
import com.starblink.android.basic.user.LoginCheckInterceptor
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.basic.util.ActivityStackManage
import java.util.*

@Route(path = "/commom/account")
class AccountProviderImp: AccountProvider {

    private var context: Context? = null

    override fun getUserToken(): String? = UserDataCenter.getToken()

    override fun getUserId(): String? = UserDataCenter.getAccountInfo()?.id

    override fun isLogin(): Boolean = UserDataCenter.isLogin()

    override fun toLogin(ifLoginAhead: (() -> Unit)?) {
        var activity: Context? = null
        try {
            activity = ActivityStackManage.getActivityStack().peek()
        } catch (e: EmptyStackException) {
            e.printStackTrace()
        }
        if (activity == null) activity = context
        activity?.let {
            LoginCheckInterceptor.check2Login(it) {
                ifLoginAhead?.invoke()
            }
        }

    }

    override fun getName(): String = "AccountProviderImp"

    override fun init(context: Context?) {
        this.context = context
    }

}